<?php


namespace app\common\model;


use think\Model;

class ConsignorItem extends Model
{
    public function memberInfo(){
        return $this->hasOne(MemberInfo::Class,'member_id');
    }

    public function setEndTimeAttr($value)
    {
        return strtotime($value);
    }
    public function getEndTimeAttr($value)
    {
        return date('Y-m-d',$value);
    }
    public function setAccessoryInfoAttr($value)
    {
        return json_encode($value);
    }
    public function getAccessoryInfoAttr($value)
    {
        return json_decode($value);
    }
    public function add($data){
        $result = $this->save($data);
        return $result;
    }
    public function getAllListByPage($paging){
        $num = null;
        if (intval($paging['num'])>0){
            $num = $paging['num'];
        }else{
            $num = 10;
        }
        $current_page = $paging['current_page'];
        $count = $this->count();
        if ($count<1){
            return [];
        }
        $total = ceil($count/$num);
        $res = $this->limit(($current_page-1)*$num,$num)->select();
        $pageInfo = [
            "current_page"=>$current_page,
            "total"=>$total
        ];
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }
    public function getListByPage($where,$paging){
        $num = null;
        if (intval($paging['num'])>0){
            $num = $paging['num'];
        }else{
            $num = 10;
        }
        $current_page = $paging['current_page'];
        $count = $this->where($where)->count();
        if ($count<1){
            return [];
        }
        $total = ceil($count/$num);
        $res = $this->where($where)->order('id','desc')->limit(($current_page-1)*$num,$num)->select();
        $pageInfo = [
            "current_page"=>$current_page,
            "total"=>$total
        ];
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }
    public function getListByTitle($title,$paging,$where){
        $num = null;
        if (intval($paging['num'])>0){
            $num = $paging['num'];
        }else{
            $num = 10;
        }
        $current_page = $paging['current_page'];
        $count = $this
            ->where('title','like','%'.$title.'%')
            ->where($where)
            ->count();
        if ($count<1){
            return [];
        }
        $total = ceil($count/$num);
        $res = $this
            ->where('title','like','%'.$title.'%')
            ->where($where)
            ->order('id', 'desc')
            ->limit(($current_page-1)*$num,$num)->select();
        $pageInfo = [
            "current_page"=>$current_page,
            "total"=>$total
        ];
        $res = $this->ConsignorItemMemberInfo($res);
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }
    public function getListByTime($title,$start_time,$end_time,$paging,$where){
        $num = null;
        if (intval($paging['num'])>0){
            $num = $paging['num'];
        }else{
            $num = 10;
        }

        $current_page = $paging['current_page'];
        $count = $this
            ->where('title','like','%'.$title.'%')
            ->where($where)
            ->whereTime('create_time','between',[$start_time,$end_time])
            ->count();
        if ($count<1){
            return [];
        }
        $total = ceil($count/$num);
        $res = $this
            ->where('title','like','%'.$title.'%')
            ->where($where)
            ->whereTime('create_time','between',[$start_time,$end_time])
            ->order('id', 'desc')
            ->limit(($current_page-1)*$num,$num)->select();
        $pageInfo = [
            "current_page"=>$current_page,
            "total"=>$total
        ];
        $res = $this->ConsignorItemMemberInfo($res);
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }
    public function getListByPageWhere($where,$paging,$whereTime,$title){
        $num = null;
        $nowDate = date('Y-m-d',time());
        if (intval($paging['num'])>0){
            $num = $paging['num'];
        }else{
            $num = 10;
        }
        $current_page = $paging['current_page'];
        $res = [];
        if($whereTime!=''){
            if($whereTime == 1){
                $count = $this->where($where)
                    ->whereTime('end_time','>=',$nowDate)
                    ->where('title','like','%'.$title.'%')
                    ->count();
                $res = $this->where($where)
                    ->where('title','like','%'.$title.'%')
                    ->whereTime('end_time','>=',$nowDate)
                    ->order('id', 'desc')
                    ->limit(($current_page-1)*$num,$num)
                    ->select();
            }
            if($whereTime == 2){
                $count = $this->where($where)
                    ->whereTime('end_time','<',$nowDate)
                    ->where('title','like','%'.$title.'%')
                    ->count();
                $res = $this->where($where)
                    ->where('title','like','%'.$title.'%')
                    ->whereTime('end_time','<',$nowDate)
                    ->order('id', 'desc')
                    ->limit(($current_page-1)*$num,$num)
                    ->select();
            }
        }else{
            $count = $this->where($where)
                ->where('title','like','%'.$title.'%')
                ->count();
            $res = $this->where($where)
                ->where('title','like','%'.$title.'%')
                ->order('id', 'desc')
                ->limit(($current_page-1)*$num,$num)
                ->select();
        }
        if ($count<1){
            return [];
        }
        $total = ceil($count/$num);
        $pageInfo = [
            "current_page"=>$current_page,
            "total"=>$total
        ];
        $res = $this->ConsignorItemMemberInfo($res);
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }
    //循环获取发布项目的会员信息
    public function ConsignorItemMemberInfo($data){
        $memberInfo = new MemberInfo();
        if(empty($data)){
            return $data;
        }
        foreach ($data as  $key => $value){
            $where = [
                'member_id'=>$value['member_id']
            ];
            $field = 'name,contacts,mobile';
            $result = $memberInfo-> getMemberInfoByIdField($where,$field);
            $data[$key]['member_name'] = $result["name"];
            $data[$key]['member_contacts'] = $result["contacts"];
            $data[$key]['member_mobile'] = $result["mobile"];
        }
        return $data;
    }
    //获取单个发布项目的会员信息
    public function ConsignorItemMemberInfoSingle($data){
        $memberInfo = new MemberInfo();
        if(empty($data)){
            return $data;
        }
        $where = [
            'member_id'=>$data['member_id']
        ];
        $field = 'name,contacts,mobile';
        $result = $memberInfo-> getMemberInfoByIdField($where,$field);
        $data['member_name'] = $result["name"];
        $data['member_contacts'] = $result["contacts"];
        $data['member_mobile'] = $result["mobile"];
        return $data;
    }
    public function updateItem($where,$data){
        $result = $this->where($where)->find()->save($data);
        return $result;
    }
    public function getItemById($id){
        $result = $this->withoutField('create_time,update_time')->find($id);
        $result = $this->ConsignorItemMemberInfoSingle($result);
        return $result;
    }
    public function getItemByWhere($where){
        $result = $this->where($where)->find();
        return $result;
    }
    public function getItemFieldById($id){
        $field = ["item_content","accessory_info"];
        $result = $this->withoutField($field)->find($id);
        return $result;
    }
    public function removeItem($id){
        $where = [
            'id'=>$id
        ];
        $result = $this->where($where)->delete();
        return $result;
    }
    public function getLimitList($where,$num){
        $result = $this->where($where)
            ->order('id','desc')
            ->field('title,id,member_id,trade_name,province,end_time')
            ->limit($num)
            ->select();
        return $result;
    }

    public function getCountByWhere($where){
        $res = $this->where($where)->count();
        return $res;
    }
}